package cn.weekendpi.util;

import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhangjw
 * @date 2019/12/19
 */
public class CodeFormatUtils {

    public static String transferParamToUUid(String string, String type,Map<String,String> map) {
        Matcher slashMatcher = Pattern.compile(type).matcher(string);
        boolean bool = false;
        StringBuilder sb = new StringBuilder();
        int indexHome = -1;
        while (slashMatcher.find()) {
            int indexEnd = slashMatcher.start();
            String tmp = string.substring(indexHome + 1, indexEnd);
            if (indexHome == -1 || !bool) {
                sb.append(tmp);
                bool = true;
                indexHome = indexEnd;
            } else {
                String tem2 = "";
                for (int i = indexEnd - 1; i > -1; i--) {
                    char c = string.charAt(i);
                    if (c == '\\') {
                        tem2 += c;
                    } else {
                        break;
                    }
                }
                int tem2Len = tem2.length();
                if(tem2Len % 2 != 1){
                    String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                    uuid = type + uuid + type;
                    map.put(uuid, type + tmp + type);
                    sb.append(uuid);
                    bool = false;
                    indexHome = indexEnd;
                }
            }
        }
        sb.append(string.substring(indexHome + 1));
        return sb.toString();
    }

}
